package com.web.service;

import com.web.VO.MyDemandVO;
import com.web.VO.MyLifeVO;
import com.web.VO.MyPublishVO;
import com.web.annotation.CurrentUser;
import com.web.dao.*;
import com.web.entity.*;
import com.web.enums.NoticeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class HomeService {
    @Resource
    private AppBannerMapper appBannerMapper;
    @Resource
    private AppNewsMapper newsMapper;
    @Resource
    private DemandInfoMapper demandInfoMapper;
    @Resource
    private UserInstantLifeMapper instantLifeMapper;
    @Resource
    private InstantLifeCommentMapper instantLifeCommentMapper;
    @Resource
    private CommentReplyMapper replyMapper;
    @Resource
    private JobMapper jobMapper;
    @Resource
    private SupplyInfoMapper supplyMapper;
    @Resource
    private ExpertsMapper expertsMapper;
    @Resource
    private NoticeMapper noticeMapper;
    @Resource
    private AskQuestionsMapper askQuestionsMapper;

    /**
     * 获取首页Banner图
     * @return
     */
    public List<AppBanner> allBanner(){
        return appBannerMapper.AllBanner();
    }

    /**
     * 获取模界头条
     * @return
     */
    public List<AppNews> allNews(){
        return newsMapper.allNews();
    }

    /**
     * 获取全部需求列表
     * @return
     */
    public List<DemandInfo> getAllDemandInfo(){
        return demandInfoMapper.getAllDemandInfo();
    }

    /**
     * 根据需求id获取需求详情
     * （获取简略需求详情）
     * @param demandId
     * @return
     */
    public DemandInfo findDemandInfoById(Integer demandId) {
        return demandInfoMapper.selectByPrimaryKey(demandId);
    }

    /**
     * 获取需求信息详情
     * @param demandId
     * @return
     */
    public DemandInfoDetails getDemandInfoDetails(Integer demandId,String userId){
        return demandInfoMapper.getDemandInfoDetails(demandId,userId);
    }

    /**
     * 发布需求
     * @param info
     */
    public void addDemand(DemandInfo info){
        info.setCreateTime(new Date());
        info.setModifyTime(new Date());
        demandInfoMapper.insertSelective(info);
    }

    /**
     * 我发布的需求
     * @param userId
     * @return
     */
    public List<MyPublishVO> listMyPublishDemand (String userId) {
        return demandInfoMapper.listMyPublishDemand(userId);
    }

    /**
     * 发布动态
     * @param life
     */
    public void addInstantLift(UserInstantLife life){
        life.setCreateTime(new Date());
        life.setModifyTime(new Date());
        instantLifeMapper.insertSelective(life);
    }

    /**
     * 获取动态列表
     * @return
     */
    public List<UserInstantLife> getUserInstantLiftList(){
        return instantLifeMapper.getInstantInfoList();
    }

    /**
     * 获取动态详情
     * @param key
     * @param value
     * @return
     */
    public UserInstantLife getUserInstantLifeDetails(String key,String value,String userId){
        UserInstantLife life = instantLifeMapper.instantInfoDetails(key,value,userId);
        if (life != null){
            life.setSeeNum(life.getSeeNum() + 1);
            instantLifeMapper.updateByPrimaryKeySelective(life);
        }
        return life;
    }

    /**
     * 分享动态
     * @param key
     * @param value
     * @param userId
     */
    public boolean instantInfoShare(String key,String value,String userId){
        UserInstantLife life = instantLifeMapper.instantInfoDetails(key,value,userId);
        if (life == null){
            return false;
        }else {
            life.setShareNum(life.getShareNum() + 1);
            instantLifeMapper.updateByPrimaryKeySelective(life);
            return true;
        }
    }

    /**
     * 评论列表获取
     * @param topicType
     * @param topicId
     * @return
     */
    public List<InstantLifeComment> getInstantLifeComment(String topicType,String topicId){
        return instantLifeCommentMapper.getCommentList(topicType,topicId);
    }

    /**
     * 添加评论
     * @param comment
     */
    public void addComment(UserInfo userInfo, InstantLifeComment comment){
        comment.setFromUid(userInfo.getUserId());
        comment.setCreateTime(new Date());
        comment.setModifyTime(new Date());
        instantLifeCommentMapper.insertSelective(comment);

        //2、发送通知
        try {
            Notice notice = new Notice();
            if (comment.getTopicType() == 1) {
                UserInstantLife userInstantLife = instantLifeMapper.findUserDynamic(comment.getTopicId());
                notice.setTarget(userInstantLife.getUserId());
            } else if (comment.getTopicType() == 2) {
                FourIdleDetails fourIdle = supplyMapper.findUserFourIdle(comment.getTopicId());
                notice.setTarget(fourIdle.getUser_id());
            } else if (comment.getTopicType() == 3) {
                UserQuiz userQuiz = askQuestionsMapper.findUserAskQuestions(comment.getTopicId());
                notice.setTarget(userQuiz.getUser_id());
            }
            notice.setSender(userInfo.getUserId());
            notice.setType(NoticeEnum.COMMENT_NOTICE.getCode());
            notice.setContent(comment.getContent());
            noticeMapper.insertSelective(notice);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取评论回复列表
     * @param commendId
     * @return
     */
    public List<CommentReplyList> getReplyList(String commendId){
        return replyMapper.getReplyList(commendId);
    }

    /**
     * 添加评论回复
     * @param commentReply
     */
    public void addReplyComment(CommentReply commentReply){
        commentReply.setCreateTime(new Date());
        commentReply.setModifyTime(new Date());
        replyMapper.insertSelective(commentReply);
    }

    /**
     * 获取评论详情
     * @param commentId
     * @return
     */
    public InstantLifeComment getCommentDetails(Integer commentId){
        return instantLifeCommentMapper.selectByPrimaryKey(commentId);
    }

    /**
     * 分享需求
     * @param key
     * @param value
     * @param userId
     * @return
     */
    public boolean instantInfoDemand(String key, String value, String userId) {
        InstantInfoDemand life = instantLifeMapper.instantInfoDemand(key,value,userId);
        if (life == null){
            return false;
        }else {
            life.setShare_num(life.getShare_num() + 1);
            demandInfoMapper.updateByPrimary(life);
            return true;
        }
    }

    /**
     * 分享招聘
     * @param key
     * @param value
     * @param userId
     * @return
     */
    public boolean instantInfoRecruitment(String key, String value, String userId) {
        InstantInfoRecruitment life = instantLifeMapper.instantInfoRecruitment(key,value,userId);
        if (life == null){
            return false;
        }else {
            life.setShare_num(life.getShare_num() + 1);
            jobMapper.updateRecruitment(life);
            return true;
        }
    }

    /**
     * 求职分享
     * @param key
     * @param value
     * @param userId
     * @return
     */
    public boolean instantInfotoApplyForAJob(String key, String value, String userId) {
        InstantInfotoApplyForAJob life = instantLifeMapper.instantInfotoApplyForAJob(key,value,userId);
        if (life == null){
            return false;
        }else {
            life.setShare_num(life.getShare_num() + 1);
            jobMapper.updateApplyForAJob(life);
            return true;
        }
    }

    /**
     * 分享临工
     * @param key
     * @param value
     * @param userId
     * @return
     */
    public boolean instantInfoTemporaryWork(String key, String value, String userId) {
        InstantInfotoApplyForAJob life = instantLifeMapper.instantInfoTemporaryWork(key,value,userId);
        if (life == null){
            return false;
        }else {
            life.setShare_num(life.getShare_num() + 1);
            jobMapper.updateApplyForAJob(life);
            return true;
        }
    }

    /**
     * 分享供应
     * @param key
     * @param value
     * @param userId
     * @return
     */
    public boolean instantInfoSupply(String key, String value, String userId) {
        InstantInfoSupply life = instantLifeMapper.instantInfoSupply(key,value,userId);
        if (life == null){
            return false;
        }else {
            life.setShare_num(life.getShare_num() + 1);
            supplyMapper.updateApplySupply(life);
            return true;
        }
    }

    /**
     * 分享四闲
     * @param key
     * @param value
     * @param userId
     * @return
     */
    public boolean instantInfoFourLeisureHours(String key, String value, String userId) {
        InstantInfoFourLeisureHours life = instantLifeMapper.instantInfoFourLeisureHours(key,value,userId);
        if (life == null){
            return false;
        }else {
            life.setShare_num(life.getShare_num() + 1);
            supplyMapper.updateFourLeisureHours(life);
            return true;
        }
    }

    /**
     * 分享专家
     * @param key
     * @param value
     * @param userId
     * @return
     */
    public boolean instantInfoExperts(String key, String value, String userId) {
        InstantInfoExperts life = instantLifeMapper.instantInfoExperts(key,value,userId);
        if (life == null){
            return false;
        }else {
            life.setShare_num(life.getShare_num() + 1);
            expertsMapper.updateExperts(life);
            return true;
        }
    }


    /**
     * 查询动态
     * @param typeId
     * @return
     */
    public UserInstantLife findUserDynamic(Integer typeId) {
        return instantLifeMapper.findUserDynamic(typeId);
    }

    /**
     * 修改动态收藏
     * @param userInstantLife
     */
    public void updateCollectionNum(UserInstantLife userInstantLife) {
        instantLifeMapper.updateCollectionNum(userInstantLife);
    }

    /**
     * 我发布的动态
     * @param userId
     * @return
     */
    public List<MyPublishVO> listMyPublishLife(String userId) {
        return instantLifeMapper.listMyPublishLife(userId);
    }

    /**
     * 查询我发布需求的详情
     * @param userId
     * @param typeId
     * @return
     */
    public MyDemand findMyDemand(String userId, Integer typeId) {
        return demandInfoMapper.findMyDemand(userId,typeId);
    }

    /**
     * 删除我发布的需求
     * @param typeId
     */
    public void DeleteMyDemand(Integer typeId) {
        demandInfoMapper.DeleteMyDemand(typeId);
    }
}
